<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:h="http://www.w3.org/1999/xhtml"
    xmlns:aorml ="http://AOR-AppML.org">
    <xsl:output method="xml" />
    <xsl:variable name="diagramType">http://b3mn.org/stencilset/UMLXClass#Diagram</xsl:variable>
    <xsl:variable name="classType">http://b3mn.org/stencilset/UMLXClass#ComplexClass</xsl:variable>
    <xsl:variable name="attributeLink">http://b3mn.org/stencilset/UMLXClass#Attribute</xsl:variable>
    <xsl:variable name="enumerationPropertyLink">http://b3mn.org/stencilset/UMLXClass#EnumerationProperty</xsl:variable>
    <xsl:variable name="operationLink">http://b3mn.org/stencilset/UMLXClass#Operation</xsl:variable>
    <xsl:variable name="enumerationLink">http://b3mn.org/stencilset/UMLXClass#Enumeration</xsl:variable>
    <xsl:variable name="enumerationLiteralLink">http://b3mn.org/stencilset/UMLXClass#EnumerationLiteral</xsl:variable>
    <xsl:variable name="navigableAssociation">http://b3mn.org/stencilset/UMLXClass#NavigableAssociation</xsl:variable>
    <xsl:variable name="biNavigabelAssociation">http://b3mn.org/stencilset/UMLXClass#BiNavigableAssociation</xsl:variable>
    <xsl:variable name="generalizationLink">http://b3mn.org/stencilset/UMLXClass#Generalization</xsl:variable>
    <xsl:variable name="parameterLink">http://b3mn.org/stencilset/UMLXClass#Parameter</xsl:variable>
    <xsl:template match="/">
        <aorml:Application xmlns:aorml ="http://AOR-AppML.org" xmlns:p="http://purl.org/dc/elements/1.1/" 
                           xmlns:xml="http://www.w3.org/XML/1998/namespace" 
                           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                           xsi:schemaLocation="http://AOR-AppML.org AOR-AppML.xsd"
                           name="{h:html/h:body/h:div[h:span=$diagramType]/h:span[@class='oryx-diagramname']}">
            <xsl:call-template name="get-classes"></xsl:call-template>
            <xsl:call-template name="get-enumerations"></xsl:call-template>
            <xsl:call-template name="get-biNaviAssociations"></xsl:call-template>
            <xsl:call-template name="get-navigableAssociations"></xsl:call-template>
        </aorml:Application>
    </xsl:template>

    <xsl:template name="get-classes">
        <xsl:for-each select="h:html/h:body/h:div[h:span=$classType]">
            <xsl:variable name="oid">
                <xsl:value-of select="@id" />
            </xsl:variable>
            <xsl:element name="aorml:ObjectType">
                <xsl:attribute name="name" >
                    <xsl:value-of select="h:span[@class='oryx-classname']" />
                </xsl:attribute>
                <xsl:for-each select="h:a[@rel='raziel-outgoing']">
                    <xsl:call-template name="get-superType">
                        <xsl:with-param name="generalization-id"
                            select="@href" />
                    </xsl:call-template>
                </xsl:for-each>
                <xsl:call-template name="get-attributes">
                    <xsl:with-param name="parent-id"
                        select="$oid" />
                </xsl:call-template>
                <xsl:call-template name="get-enumerationProperties">
                    <xsl:with-param name="parent-id"
                        select="$oid" />
                </xsl:call-template>
            </xsl:element>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="get-attributes">
        <xsl:param name="parent-id"></xsl:param>
        <xsl:variable name="parent-string"><xsl:value-of select="concat('#' , $parent-id)" /></xsl:variable>
        <xsl:for-each
            select="/h:html/h:body/h:div[h:span=$attributeLink][h:a[@rel='raziel-parent']][h:a[@href=$parent-string]]">
            <xsl:sort select="substring-before(substring-after(h:span[@class='oryx-bounds'],','),',')" data-type="number"></xsl:sort>
            <xsl:element name="aorml:Attribute"> 
                <xsl:attribute name="name">
                    <xsl:value-of select="h:span[@class='oryx-name']"></xsl:value-of>
                </xsl:attribute>
                <xsl:attribute name="type">
                    <xsl:value-of select="h:span[@class='oryx-datatype']"></xsl:value-of>
                </xsl:attribute>
                <xsl:if test="(h:span[@class='oryx-isstdid'] != '') and (h:span[@class='oryx-isstdid'] != 'false')">
                    <xsl:attribute name="isStandardID" >
                        <xsl:text>true</xsl:text>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:if test="h:span[@class='oryx-initialvalue'] != ''">
                    <xsl:attribute name="initialValue" >
                        <xsl:value-of select="h:span[@class='oryx-initialvalue']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:if test="h:span[@class='oryx-minvalue'] != ''">
                    <xsl:attribute name="minValue" >
                        <xsl:value-of select="h:span[@class='oryx-minvalue']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:if test="h:span[@class='oryx-maxvalue'] != ''">
                    <xsl:attribute name="maxValue" >
                        <xsl:value-of select="h:span[@class='oryx-maxvalue']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:if test="(h:span[@class='oryx-uppermultiplicity'] != '') and (h:span[@class='oryx-uppermultiplicity'] != '1')">
                    <xsl:attribute name="upperMultiplicity" >
                        <xsl:value-of select="h:span[@class='oryx-uppermultiplicity']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:if test="(h:span[@class='oryx-lowermultiplicity'] != '') and (h:span[@class='oryx-lowermultiplicity'] != '1')">
                    <xsl:attribute name="lowerMultiplicity" >
                        <xsl:value-of select="h:span[@class='oryx-lowermultiplicity']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:if test="(h:span[@class='oryx-isinfo'] != '') and (h:span[@class='oryx-isinfo'] != 'false')">
                    <xsl:attribute name="isInfo" >
                        <xsl:value-of select="h:span[@class='oryx-isinfo']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:if test="h:span[@class='oryx-label'] != ''">
                    <xsl:attribute name="label" >
                        <xsl:value-of select="h:span[@class='oryx-label']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:if test="h:span[@class='oryx-typesize'] != ''">
                    <xsl:attribute name="typeSize" >
                        <xsl:value-of select="h:span[@class='oryx-typesize']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
            </xsl:element>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="get-enumerationProperties">
        <xsl:param name="parent-id"></xsl:param>
        <xsl:variable name="parent-string">
            <xsl:value-of select="concat('#' , $parent-id)" />
        </xsl:variable>
        <xsl:for-each
            select="/h:html/h:body/h:div[h:span=$enumerationPropertyLink][h:a[@rel='raziel-parent']][h:a[@href=$parent-string]]">

            <xsl:element name="aorml:EnumerationProperty">
                <xsl:attribute name="name">
                    <xsl:value-of select="h:span[@class='oryx-name']" />
                </xsl:attribute>
                <xsl:attribute name="type">
                    <xsl:value-of select="h:span[@class='oryx-enumeration']" />
                </xsl:attribute>
                <xsl:if test="h:span[@class='initialvalue'] != ''">
                    <xsl:attribute name="initialValue" >
                        <xsl:value-of select="h:span[@class='initialvalue']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:if test="(h:span[@class='oryx-uppermultiplicity'] != '') and (h:span[@class='oryx-uppermultiplicity'] != '1')">
                    <xsl:attribute name="upperMultiplicity" >
                        <xsl:value-of select="h:span[@class='oryx-uppermultiplicity']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:if test="(h:span[@class='oryx-lowermultiplicity'] != '') and (h:span[@class='oryx-lowermultiplicity'] != '1')">
                    <xsl:attribute name="lowerMultiplicity" >
                        <xsl:value-of select="h:span[@class='oryx-lowermultiplicity']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:if test="h:span[@class='oryx-label'] != ''">
                    <xsl:attribute name="label" >
                        <xsl:value-of select="h:span[@class='oryx-label']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
            </xsl:element>
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template name="get-enumerations">
        <xsl:for-each select="h:html/h:body/h:div[h:span=$enumerationLink]">
            <xsl:variable name="oid">
                <xsl:value-of select="@id" />
            </xsl:variable>
            <aorml:Enumeration name="{h:span[@class='oryx-name']}">
                <xsl:call-template name="get-enumerationLiteral">
                    <xsl:with-param name="parent-id"
                        select="$oid" />
                </xsl:call-template>                
            </aorml:Enumeration>
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template name="get-enumerationLiteral">
        <xsl:param name="parent-id"></xsl:param>
        <xsl:variable name="parent-string">
            <xsl:value-of select="concat('#' , $parent-id)" />
        </xsl:variable>
        <xsl:for-each
            select="/h:html/h:body/h:div[h:span=$enumerationLiteralLink][h:a[@rel='raziel-parent']][h:a[@href=$parent-string]]">

            <xsl:element name="aorml:EnumerationLiteral">                              
                <xsl:if test="h:span[@class='oryx-displaystring'] != ''">
                    <xsl:attribute name="displayString" >
                        <xsl:value-of select="h:span[@class='oryx-displaystring']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:variable name="literal" select="h:span[@class='oryx-literal']"></xsl:variable>          
                <xsl:value-of select="translate($literal, 
                                'abcdefghijklmnopqrstuvwxyz', 
                                'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"></xsl:value-of>
            </xsl:element>
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template name="get-biNaviAssociations">
        <xsl:for-each select="h:html/h:body/h:div[h:span=$biNavigabelAssociation]">
            <xsl:variable name="selfID"><xsl:value-of select="concat('#' , @id)" /></xsl:variable>
            <xsl:variable name="sourceName"><xsl:value-of select="//h:div[h:a[@rel='raziel-outgoing' and @href=$selfID]]/h:span[@class='oryx-classname']"/></xsl:variable>
            <xsl:variable name="targetID"><xsl:value-of select="h:a/@href[../@rel='raziel-target']" /> </xsl:variable>
            <xsl:variable name="targetName"><xsl:value-of select="//h:div[concat('#' , @id)=$targetID]/h:span[@class='oryx-classname']" /> </xsl:variable>
            <xsl:element name="aorml:BiNavigableAssociation">  
                <xsl:if test="h:span[@class='oryx-name'] != ''">
                    <xsl:attribute name="name" >
                        <xsl:value-of select="h:span[@class='oryx-name']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:attribute name="domain">
                    <xsl:value-of select="$sourceName"></xsl:value-of>
                </xsl:attribute>
                <xsl:attribute name="range" >
                    <xsl:value-of select="$targetName"></xsl:value-of>
                </xsl:attribute>
                <xsl:if test="h:span[@class='oryx-domainrole'] != ''">
                    <xsl:attribute name="domainRoleName" >
                        <xsl:value-of select="h:span[@class='oryx-domainrole']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:if test="h:span[@class='oryx-rangerole'] != ''">
                    <xsl:attribute name="rangeRoleName">
                        <xsl:value-of select="h:span[@class='oryx-rangerole']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:if test="h:span[@class='oryx-domainmultiplicity'] != ''">
                    <xsl:attribute name="domainMultiplicity" >
                        <xsl:value-of select="h:span[@class='oryx-domainmultiplicity']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:if test="h:span[@class='oryx-rangemultiplicity'] != ''">
                    <xsl:attribute name="rangeMultiplicity" >
                        <xsl:value-of select="h:span[@class='oryx-rangemultiplicity']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
            </xsl:element>
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template name="get-navigableAssociations">
        <xsl:for-each select="h:html/h:body/h:div[h:span=$navigableAssociation]">
            <xsl:variable name="selfID"><xsl:value-of select="concat('#' , @id)" /></xsl:variable>
            <xsl:variable name="sourceName"><xsl:value-of select="//h:div[h:a[@rel='raziel-outgoing' and @href=$selfID]]/h:span[@class='oryx-classname']"/></xsl:variable>
            <xsl:variable name="targetID"><xsl:value-of select="h:a/@href[../@rel='raziel-target']" /> </xsl:variable>
            <xsl:variable name="targetName"><xsl:value-of select="//h:div[concat('#' , @id)=$targetID]/h:span[@class='oryx-classname']" /> </xsl:variable>
            <xsl:element name="aorml:NavigableAssociation">  
                <xsl:if test="h:span[@class='oryx-name'] != ''">
                    <xsl:attribute name="name" >
                        <xsl:value-of select="h:span[@class='oryx-name']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:attribute name="domain">
                    <xsl:value-of select="$sourceName"></xsl:value-of>
                </xsl:attribute>
                <xsl:attribute name="range" >
                    <xsl:value-of select="$targetName"></xsl:value-of>
                </xsl:attribute>
                <xsl:if test="h:span[@class='oryx-domainrole'] != ''">
                    <xsl:attribute name="domainRoleName" >
                        <xsl:value-of select="h:span[@class='oryx-domainrole']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:if test="h:span[@class='oryx-rangerole'] != ''">
                    <xsl:attribute name="rangeRoleName">
                        <xsl:value-of select="h:span[@class='oryx-rangerole']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:if test="h:span[@class='oryx-domainmultiplicity'] != ''">
                    <xsl:attribute name="domainMultiplicity" >
                        <xsl:value-of select="h:span[@class='oryx-domainmultiplicity']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:if test="h:span[@class='oryx-rangemultiplicity'] != '' ">
                    <xsl:attribute name="rangeMultiplicity" >
                        <xsl:value-of select="h:span[@class='oryx-rangemultiplicity']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
                <xsl:if test="h:span[@class='oryx-isinversenavigable'] != ''">
                    <xsl:attribute name="isInverseNavigable" >
                        <xsl:value-of select="h:span[@class='oryx-isinversenavigable']"></xsl:value-of>
                    </xsl:attribute> 
                </xsl:if>
            </xsl:element>
            
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template name="get-superType">
        <xsl:param name="generalization-id"></xsl:param>
        <xsl:variable name="superTypeID">
            <xsl:value-of select="/h:html/h:body/h:div[h:span=$generalizationLink][concat('#', @id)=$generalization-id]/h:a[@rel='raziel-target']/@href" />
        </xsl:variable>
        <xsl:if test="$superTypeID != ''">
            <xsl:attribute name="superType">
                <xsl:value-of select="/h:html/h:body/h:div[h:span=$classType][concat('#', @id)=$superTypeID]/h:span[@class='oryx-classname']"></xsl:value-of>
            </xsl:attribute>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>